﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using YiSha.Data.Repository;
using YiSha.Entity.Business;
using YiSha.Entity.OrganizationManage;
using YiSha.Model.Param.Business;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;

namespace YiSha.Service.OrganizationManage
{
    public class PersonService : RepositoryFactory
    {
        private DepartmentService departmentService = new DepartmentService();
        #region 获取数据
        public async Task<List<PersonEntity>> GetList(PersonListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<PersonEntity>> GetPageList(PersonListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<PersonEntity> GetEntity(long id)
        {

            var model = this.BaseRepository().FindEntity<PersonEntity>(id);

            if (model.Result.DepartmentId > 0)
            {
                DepartmentEntity departmentEntity = await departmentService.GetEntity(model.Result.DepartmentId);
                if (departmentEntity != null)
                {
                    model.Result.DepartmentName = departmentEntity.DepartmentName;
                }
            }

            return await model;
        }



        public bool ExistPersonName(PersonEntity entity)
        {
            var expression = LinqExtensions.True<PersonEntity>();
            expression = expression.And(t => t.BaseIsDelete == 0);
            if (entity.Id.IsNullOrZero())
            {
                expression = expression.And(t => t.RealName == entity.RealName && t.Mobile == entity.Mobile);
            }
            else
            {
                expression = expression.And(t => t.RealName == entity.RealName && t.Mobile == entity.Mobile && t.Id != entity.Id);
            }
            return this.BaseRepository().IQueryable(expression).Count() > 0 ? true : false;
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(PersonEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                await entity.Create();
                await this.BaseRepository().Insert<PersonEntity>(entity);
            }
            else
            {
                await entity.Modify();
                await this.BaseRepository().Update(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<PersonEntity>(idArr);
        }
        #endregion

        #region 私有方法
        private Expression<Func<PersonEntity, bool>> ListFilter(PersonListParam param)
        {
            var expression = LinqExtensions.True<PersonEntity>();
            if (param != null)
            {
                if (!string.IsNullOrEmpty(param.Key))
                {
                    expression = expression.And(t => t.RealName.Contains(param.Key) || t.ContactsName.Contains(param.Key) || t.Mobile.Contains(param.Key) || t.RealNameEn.Contains(param.Key));
                }




                if (!string.IsNullOrEmpty(param.DepartmentId))
                {
                    var dps = Convert.ToInt64(param.DepartmentId.Split(",").Last());
                    var departmentEntity = departmentService.GetList(dps);


                    expression = expression.And(t => departmentEntity.Result.Select(e => e.Id).ToList().Contains(t.DepartmentId) || t.DepartmentId == dps);
                }

            }



            return expression;
        }
        #endregion
    }
}
